/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hcsc.datapower.CMDB.daoImpl;

import com.hcsc.datapower.CMDB.dao.AbstractDao;
import com.hcsc.datapower.CMDB.dao.DomainDao;
import com.hcsc.datapower.CMDB.integration.Domain;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author I307211
 */
public class DomainDaoImpl extends AbstractDao implements DomainDao {
    
    private static final String TABLE = "Domains";
       
    @Override
    public Domain getDomain(String name) {
        String sql = "SELECT * FROM " + TABLE + " WHERE Domain_name= ?";
        return (Domain)this.jdbcTemplate.query(sql,new Object[] { name }, new DomainMapper());
    }
    
    @Override
    public List<Domain> getDomainsByPlatform(String platform) {
        
        //you could write the SQL...this is easier for now..
        List<Domain> tmp = new ArrayList<Domain>();
        List<Domain> all = getAllDomains();
        
        Iterator itr = all.iterator();
        while(itr.hasNext()){
            Domain t = (Domain)itr.next();
            if(t.getPlatform().equalsIgnoreCase(platform)){
                tmp.add(t);
            }
        }
        
        if(!tmp.isEmpty())
            return tmp;
        return null;    
    }

    @Override
    public int createDomain(Domain domain) {
        String sql = "INSERT INTO " + TABLE +" Values ( ?, ? , ?, ? ) ";
        return jdbcTemplate.update(sql, new Object[] {
            domain.getDomain_name(),
            domain.getPlatform(),
            domain.getWhoUpdated(),
            domain.getTimeUpdated()
        });
    }

    @Override
    public int deleteDomain(Domain domain) {
        String sql = "DELETE FROM Users WHERE Domain_name = ? ";
        return jdbcTemplate.update(sql, new Object[] { domain.getDomain_name() });
    }

    @Override
    public int updateDomain(Domain domain) {
            String sql = "UPDATE " + TABLE + " SET " +
            "Domain_name= ?" +
            "Platform = ? " +
            "WhoUpdated = ? " +
            "TimeUpdated =?" ;
            return jdbcTemplate.update(sql, new Object[] { 
            domain.getDomain_name(), 
            domain.getPlatform(), 
            domain.getWhoUpdated(), 
            domain.getTimeUpdated()
            });
    }

    @Override
    public List<Domain> getAllDomains() {
        String sql = "SELECT * FROM " + TABLE ;
        List<Domain> tmp = this.jdbcTemplate.query(sql, new DomainMapper());
        
        if(tmp!=null)
            return tmp;
        return null;
    }
    
     private static final class DomainMapper implements RowMapper {

        public Object mapRow(ResultSet rs, int i) throws SQLException {
                Domain d = new Domain();
                d.setDomain_name(rs.getString("Domain_name"));
                d.setPlatform(rs.getString("Platform"));
                d.setWhoUpdated(rs.getString("WhoUpdated"));
                d.setTimeUpdated(rs.getDate("TimeUpdated"));
                return d;
        }
    }
}
